#include <stm32l1xx.h>
#include "cmsis_os.h"


void Thread_1(void const *arg);
void Thread_2(void const *arg);

osThreadDef (Thread_1, osPriorityNormal, 1, 0); 
osThreadDef (Thread_2, osPriorityNormal, 1, 0); 


void usart_config()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef  NVIC_InitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
	

	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_40MHz;
	GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10|GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
  
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1 );
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1 );
	
	USART_InitStructure.USART_BaudRate=9600;
	USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStructure.USART_StopBits=USART_StopBits_1;
	USART_InitStructure.USART_WordLength=USART_WordLength_8b;
	USART_InitStructure.USART_Parity=USART_Parity_No;
	USART_Init(USART1, &USART_InitStructure);
	
	
	
	NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd= ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
	NVIC_Init(&NVIC_InitStruct);
	USART_ITConfig(USART1, USART_IT_ORE_RX , ENABLE);
	USART_Cmd(USART1,ENABLE);
}

 void GPIO_config()
 {
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOB,GPIO_Pin_7);
 
 }
 void Thread_2(void const *arg)
{
 for(;;)
	{
	 GPIO_SetBits(GPIOB,GPIO_Pin_6);
	 osDelay (90);
   GPIO_ResetBits(GPIOB,GPIO_Pin_6);
   osDelay (90); 		
	}
}
void Thread_1(void const *arg)
{

   for(;;)
	{
	 GPIO_SetBits(GPIOB,GPIO_Pin_7);
	 osDelay (50);
   GPIO_ResetBits(GPIOB,GPIO_Pin_7);
   osDelay (50); 
	}
	
}
 int main(void)
{
	

	GPIO_config();
	usart_config();
  osKernelInitialize (); 
	osThreadCreate (osThread (Thread_1), NULL);  
	osThreadCreate (osThread (Thread_2), NULL); 
	osKernelStart(); 
	for(;;);

}
